import { createRouter, createWebHistory } from "vue-router";
import loginRouters from "./components/login";
import layoutRouters from "./components/layout";
import chartRouters from "./components/chart";

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHistory(),
  routes: [...loginRouters, ...layoutRouters, ...chartRouters],
  scrollBehavior(to, from, savedPosition) {
    return savedPosition || { top: 0 };
  },
});

// 全局路由前置守卫
router.beforeEach((to, form, next) => {
  const toMatched = to.matched;
  if (toMatched.length === 0) {
    // 路由未匹配
    next({ name: "404" });
  } else {
    // if (无权限) {
    //   next({ name: "login" });
    // }
    next();
  }
});

export default router;
